<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 5/18/14
 * Time: 1:57 PM
 */

namespace controller;

include_once("Parcer.php");
include_once("Mapping.php");

class ActionParcer implements Parcer
{

  static function parseActions($parseArray)
  {
    $splMappingMap = new \SplObjectStorage();
    $splRedirectMap = new \SplObjectStorage();

    $arrayLength = count($parseArray);
    for ($i = 0; $i < $arrayLength; $i++) {
      $mapping = new Mapping();
      $action = ActionParcer::getAction($parseArray[$i]);
      if ($action) {
        $mapping->setAction($action);
        $mapping->setFrom(ActionParcer::getFrom($parseArray[$i]));
        $splMappingMap->attach($mapping);
      } else {
        $action = ActionParcer::getRedirect($parseArray[$i]);
        if ($action) {
          $mapping->setAction($action);
          $mapping->setFrom(ActionParcer::getRedirectFrom($parseArray[$i]));
          $splRedirectMap->attach($mapping);
        }
      }
    }
    return $splMappingMap;
  }

  static function parseRedirect($parseArray)
  {
    $splRedirectMap = new \SplObjectStorage();

    $arrayLength = count($parseArray);
    for ($i = 0; $i < $arrayLength; $i++) {
      $mapping = new Mapping();
      $action = ActionParcer::getRedirect($parseArray[$i]);
      if ($action) {
        $mapping->setAction($action);
        $mapping->setFrom(ActionParcer::getRedirectFrom($parseArray[$i]));
        $splRedirectMap->attach($mapping);
      }
    }
    return $splRedirectMap;
  }

  private static function getAction($parseString)
  {
    $actionStartPos = stripos($parseString, "apping");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = $actionStartPos + 7;
    $actionEndPos = stripos($parseString, "=", $actionStartPos);
    $action = trim(substr($parseString, $actionStartPos, $actionEndPos - $actionStartPos)) . ".php";
    return $action;
  }

  private static function getFrom($parseString)
  {
    $actionStartPos = stripos($parseString, "apping");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = stripos($parseString, "=") + 1;
    $action = trim(substr($parseString, $actionStartPos));

    return $action;
  }

  private static function getRedirect($parseString)
  {
    $actionStartPos = stripos($parseString, "irect");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = $actionStartPos + 8;
    $scopeStartPos = stripos($parseString, ".");
    $scopeEndPos = stripos($parseString, ".", $scopeStartPos + 1);
    $scope = trim(substr($parseString, $scopeStartPos + 1, $scopeEndPos - $scopeStartPos - 1));

    switch ($scope) {
      case "root":
        $scope = "";
        break;
      default:
        $scope = "";
        break;
    }

    $actionEndPos = stripos($parseString, "=", $actionStartPos) - 1;
    $action = trim(substr($parseString, $scopeEndPos + 1, $actionEndPos - $scopeEndPos));
    return $scope . $action;
  }

  private static function getRedirectFrom($parseString)
  {
    $actionStartPos = stripos($parseString, "irect");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = stripos($parseString, "=") + 1;
    $action = trim(substr($parseString, $actionStartPos));

    return $action;
  }

}